<template>
  <div class="profile">
    <!-- 未登录头部 -->
    <div v-if="!user" class="not_login header">
      <img @click="$router.push('/login')" src="@/assets/images/mobile.png" alt="" />
      <span>登录 / 注册</span>
    </div>
    <!-- / 未登录头部 -->

    <!-- 已登录的头部 -->
    <div v-else class="login-box header">
      <!-- 用户头像 -->
      <div class="userInfo">
        <div class="left">
          <img
            :src='userInfo.photo'
            alt=""
          />
          <span>{{userInfo.name}}</span>
        </div>
        <van-button to='/user' type="default">编辑资料</van-button>
      </div>

      <!-- 用户数据 -->
      <div class="userData">
        <div class="data-item">
          <span>{{userInfo.art_count}}</span>
          <span>头条</span>
        </div>
        <div class="data-item">
          <span>{{userInfo.fans_count}}</span>
          <span>粉丝</span>
        </div>
        <div class="data-item">
          <span>{{userInfo.follow_count}}</span>
          <span>关注</span>
        </div>
        <div class="data-item">
      <span>{{userInfo.like_count}}</span>
          <span>获赞</span>
        </div>
      </div>
    </div>
    <!-- / 已登录的头部 -->

    <!-- 收藏 -->
    <van-grid class="myGrid" :column-num="2">
      <van-grid-item icon="photo-o" text="收藏">
        <template #icon><i class="toutiao toutiao-shoucang"></i></template>
      </van-grid-item>
      <van-grid-item icon="photo-o" text="历史">
        <template #icon><i class="toutiao toutiao-lishi"></i></template>
      </van-grid-item>
    </van-grid>
    <!--  /收藏 -->

    <!-- 其他导航 -->
  <div class='out-box'>
    <van-cell title="消息通知" is-link />
  <van-cell title="小智同学"  is-link/>
  </div>
  <van-cell v-if="user" title="退出" class="logout-btn " @click="onLogout"  />
    <!-- /其他导航 -->
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { Dialog, Toast } from 'vant'
import { getUserSelfInfo } from '@/api/user'
export default {
  data () {
    return {
      userInfo: {}
    }
  },

  created () {
    if (this.user) {
      this.loadUser()
    }
  },

  computed: {
    ...mapState(['user'])
  },
  methods: {
    onLogout () {
      Dialog.confirm({
        title: '黑马头条',
        message: '是否确认退出该账号'
      })
        .then(() => {
          this.$store.commit('removeUser')
        })
        .catch(() => {})
    },
    async loadUser () {
      try {
        const res = await getUserSelfInfo()
        this.userInfo = res
      } catch (err) {
        Toast('获取失败')
      }
    }
  }
}
</script>

<style scoped lang="less">
.profile {
  // 公共样式
  .header {
    width: 750px;
    height: 401px;
    background-image: url(~@/assets/images/banner.png);
    background-size: 100% 100%;
  }
  // 未登录样式
  .not_login {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    img {
      width: 132px;
      height: 132px;
    }
    span {
      font-size: 28px;
      color: #fff;
      margin-top: 10px;
    }
  }

  // 已登录样式
  .login-box {
    overflow: hidden;
    .userInfo {
      box-sizing: border-box;
      width: 100%;
      height: 132px;
      margin-top: 116px;
      padding: 0 32px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .left {
        display: flex;
        align-items: center;

        img {
          width: 132px;
          height: 132px;
          border-radius: 50%;
          border: 1px solid #fff;
        }
        span {
          font-size: 30px;
          color: #fff;
          margin-left: 22px;
        }
      }
      .van-button {
        width: 115px;
        height: 38px;
        background: #ffffff;
        border-radius: 16px;
        font-size: 20px;
        color: #666;
        padding: 0 4px;
      }
    }
    .userData {
      width: 100%;
      height: 156px;
      display: flex;
      .data-item {
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: center;
        flex-direction: column;
        color: #fff;
        span:nth-child(1) {
          font-size: 26px;
          margin-bottom: 8px;
        }
        span:nth-child(2) {
          font-size: 22px;
        }
      }
    }
  }

  //收藏
 .myGrid {
    .toutiao {
      font-size: 45px;
    }

    .toutiao-shoucang {
      color: #EB5253;
    }

    .toutiao-lishi {
      color: #FF9D1D;
    }

  }

}
.out-box{
margin:10px 0 ;
}
.logout-btn{
  text-align: center;
  color: red;
}
</style>
